$(function () {
    var logFlag = true;
    var reg = /^1[34578]\d{9}$/;
    var obj = {
        init: function () {
            this.events();
            this.initDate();
            // this.checkLogin();
            this.checkRegister();
            console.log(this.getCookie("rem"));
            if (this.getCookie("rem") == "true") {
                $(".login").removeAttr("disabled");
                $("input[name='loginTel']").val(this.getCookie("username"));
                $("input[name='loginPasswd']").val(this.getCookie("passwd"));
                $("input[name='rem']").attr("checked", true);
            } else {
                console.log("err");
                console.log(this.getCookie("username"));
                console.log(this.getCookie("passwd"));
            }
        },
        events: function () {
            var that = this;
            $(".login").click(function () {
                var tel = $("input[name='loginTel']").val();
                var loginPasswd = $("input[name='loginPasswd']").val();
                console.log(tel, " : ", loginPasswd);
                $.ajax({
                    type: "post",
                    url: "http://127.0.0.1:8888/linkMan/linkman/login",
                    data: {
                        tel: tel,
                        passwd: loginPasswd
                    },
                    dataType: "json",
                    success: function (res) {
                        console.log("login res: ", res);
                        if (res.code == "200") {
                            sessionStorage.setItem('sessionId', res.sessionId);
                            sessionStorage.setItem('flag', true);
                            sessionStorage.setItem("tel", tel);
                            sessionStorage.setItem("username", res.username);
                            // $(".rem").is(":checked") || $(".rem")[0].checked
                            if ($("input[name='rem']:checked").val() != null) {
                                that.setCookie("rem", true, 7);
                                that.setCookie('username', '13111111111', 7);
                                that.setCookie('passwd', '123123', 7);
                            } else {
                                that.setCookie("rem", false, 7);
                            }
                            location.href = "../index.html"
                        } else {
                            $(".tips").html(res.msg);
                            alert(res.msg);
                        }
                    }
                });
            });

            $(".register").click(function () {
                var registerName = $("input[name='registerName']").val();
                var registerPasswd = $("input[name='registerPasswd']").val();
                var registerTel = $("input[name='registerTel']").val();
                var registerAddr = $("input[name='registerAddr']").val();
                var registerBirthday = $("input[name='registerBirthday']").val();
                var gender = $("input[name='gender']:checked").val();
                var sex;
                if (gender == "1") {
                    sex = "男";
                } else {
                    sex = "女";
                }

                $.ajax({
                    type: "post",
                    url: "http://127.0.0.1:8888/linkMan/linkman/register",
                    data: {
                        username: registerName,
                        passwd: registerPasswd,
                        tel: registerTel,
                        birthday: registerBirthday,
                        addr: registerAddr,
                        gender: sex
                    },
                    dataType: "json",
                    success: function (res) {
                        console.log("login res: ", res);
                        if (res.code = "200") {
                            if (confirm(res.msg + ",点击确定跳转到登录页面!")) {
                                $(".loginView").css("display", "block");
                                $(".registerView").css("display", "none");
                                $(".registerView form")[0].reset();
                                $(".loginTab").click();
                            } else {
                                $(".registerView form")[0].reset();
                            }
                        }
                    }
                });

            });

            $(".loginTab").click(function () {
                logFlag = true;
                $(".loginView").css("display", "block");
                $(".registerView").css("display", "none");
                $(".registerView form")[0].reset();
                $(".register").attr("disabled", "true");
            });

            $(".registerTab").click(function () {
                logFlag = false;
                $(".loginView").css("display", "none");
                $(".registerView").css("display", "block");
                // that.initDate();
                $(".loginView form")[0].reset();
                $(".login").attr("disabled", "true");
            });

            //判断是否敲击了Enter键 
            $(document).keyup(function (event) {
                if (event.keyCode == 13) {
                    if (logFlag) {
                        $(".login").trigger("click");
                    } else {
                        $(".register").trigger("click");
                    }
                }
            });
        },
        initDate: function () {
            var date_now = new Date();
            var year = date_now.getFullYear();
            var month = date_now.getMonth() + 1 < 10 ? "0" + (date_now.getMonth() + 1) : (date_now.getMonth() + 1);
            var date = date_now.getDate() < 10 ? "0" + date_now.getDate() : date_now.getDate();
            var disDate = year + "-" + month + "-" + date;
            //设置input标签的max属性
            $("input[name='registerBirthday']").attr("max", disDate);
            $("input[name='registerBirthday']").attr("value", disDate);
        },
        checkLogin: function () {
            var that = this;
            var passwd = "";

            $("input[name='loginTel']")[0].addEventListener("input", function () {
                tel = $("input[name='loginTel']").val();
                var telFlag = that.checkTel(tel);
                var passwdFlag = that.checkPasswd(passwd);
                if (telFlag) {
                    $(".telTip").removeClass("glyphicon-info-sign");
                    $(".telTip").addClass("glyphicon-ok-circle");
                    $(".telTip").css({
                        "color": "green",
                        "font-size": "18px"
                    });
                } else {
                    $(".telTip").removeClass("glyphicon-ok-circle");
                    $(".telTip").addClass("glyphicon-info-sign");
                    $(".telTip").css({
                        "color": "red",
                        "font-size": "18px"
                    });
                }
                if (passwdFlag && telFlag) {
                    $(".login").removeAttr("disabled");
                } else {
                    $(".login").attr("disabled", true);
                }
            })
            $("input[name='loginPasswd']")[0].addEventListener("input", function () {
                passwd = $("input[name='loginPasswd']").val();
                var telFlag = that.checkTel(tel);
                var passwdFlag = that.checkPasswd(passwd);
                if (that.checkPasswd(passwd)) {
                    $(".passwdTip").removeClass("glyphicon-info-sign");
                    $(".passwdTip").addClass("glyphicon-ok-circle");
                    $(".passwdTip").css({
                        "color": "green",
                        "font-size": "18px"
                    });
                } else {
                    $(".passwdTip").removeClass("glyphicon-ok-circle");
                    $(".passwdTip").addClass("glyphicon-info-sign");
                    $(".passwdTip").css({
                        "color": "red",
                        "font-size": "18px"
                    });
                }
                if (passwdFlag && telFlag) {
                    $(".login").removeAttr("disabled");
                } else {
                    $(".login").attr("disabled", true);
                }
            })
        },
        checkRegister: function (passwd) {
            var that = this;
            var telEle = $("input[name='registerTel']");
            var usernameEle = $("input[name='registerName']");
            var passwdEle = $("input[name='registerPasswd']");
            var telFlag = false;
            var nameFlag = false;
            var passwdFlag = false;
            telEle.on("input", function () {
                telFlag = that.checkTel(telEle.val());
                if (telFlag) {
                    $(".reTelTip").removeClass("glyphicon-asterisk")
                    $(".reTelTip").addClass(" glyphicon-ok-circle");
                    $(".reTelTip").css({
                        "color": "green",
                        "font-size": "18px"
                    });
                } else {
                    $(".reTelTip").removeClass(" glyphicon-ok-circle")
                    $(".reTelTip").addClass("glyphicon-info-sign");
                    $(".reTelTip").css({
                        "color": "red",
                        "font-size": "18px"
                    });
                }
                if (telFlag && nameFlag && passwdFlag) {
                    $(".register").removeAttr("disabled");
                } else {
                    $(".register").attr("disabled", true);
                }
            });
            usernameEle.on("input", function () {
                nameFlag = that.checkUsername(usernameEle.val());
                if (nameFlag) {
                    $(".reNameTip").removeClass("glyphicon-asterisk")
                    $(".reNameTip").addClass("glyphicon-ok-circle");
                    $(".reNameTip").css({
                        "color": "green",
                        "font-size": "18px"
                    });

                } else {
                    $(".reNameTip").removeClass(" glyphicon-ok-circle")
                    $(".reNameTip").addClass("glyphicon-info-sign");
                    $(".reNameTip").css({
                        "color": "red",
                        "font-size": "18px"
                    });
                }
                if (telFlag && nameFlag && passwdFlag) {
                    $(".register").removeAttr("disabled");
                } else {
                    $(".register").attr("disabled", true);
                }
            });
            passwdEle.on("input", function () {
                passwdFlag = that.checkPasswd(passwdEle.val());
                if (passwdFlag) {
                    $(".rePasswdTip").removeClass("glyphicon-asterisk")
                    $(".rePasswdTip").addClass("glyphicon-ok-circle");
                    $(".rePasswdTip").css({
                        "color": "green",
                        "font-size": "18px"
                    });
                } else {
                    $(".rePasswdTip").removeClass(" glyphicon-ok-circle")
                    $(".rePasswdTip").addClass("glyphicon-info-sign");
                    $(".rePasswdTip").css({
                        "color": "red",
                        "font-size": "18px"
                    });
                }
                if (telFlag && nameFlag && passwdFlag) {
                    $(".register").removeAttr("disabled");
                } else {
                    $(".register").attr("disabled", true);
                }
            });

        },
        checkTel: function (tel) {
            if (!(reg.test(tel))) {
                return false;
            } else {
                return true;
            }
        },
        checkPasswd: function (passwd) {
            if (passwd.length < 6) {
                return false;
            } else {
                return true;
            }
        },
        checkUsername: function (username) {
            if (username == null || username == undefined || username == "" || (username.trim()).length == 0) {
                return false;
            } else {
                return true;
            }
        },
        setCookie: function (c_name, value, expiredays) {
            var exdate = new Date()
            exdate.setDate(exdate.getDate() + expiredays)
            document.cookie = c_name + "=" + escape(value) +
                ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
        },
        getCookie: function (c_name) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=")
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1
                    c_end = document.cookie.indexOf(";", c_start)
                    if (c_end == -1) c_end = document.cookie.length
                    return unescape(document.cookie.substring(c_start, c_end))
                }
            }
            return ""
        }

    }
    obj.init();
})